package cn.com.ddapp.ddm.ui.dialog;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.util.TDevice;


public class SmileFaceDialog extends Dialog {
	public OnClickListener listener;
	protected View content;
	private final int contentPadding;
	private ImageView deleteIcon;

	protected OnClickListener dismissClick = new OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
		}
	};

	public SmileFaceDialog(Context context) {
		this(context, R.style.dialog_common);
	}

	public SmileFaceDialog(Context context, int defStyle) {
		super(context, defStyle);
		contentPadding = (int) getContext().getResources().getDimension(
				R.dimen.global_dialog_padding);
		init(context);
	}

	protected SmileFaceDialog(Context context, boolean flag,
							  OnCancelListener listener) {
		super(context, flag, listener);
		contentPadding = (int) getContext().getResources().getDimension(
				R.dimen.global_dialog_padding);
		init(context);
	}

	@SuppressLint("InflateParams")
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	protected void init(final Context context) {
		setCancelable(false);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		content = LayoutInflater.from(context).inflate(
				R.layout.smileface_dialog_common, null);
		deleteIcon = (ImageView) content.findViewById(R.id.delete_icon);
		deleteIcon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				SmileFaceDialog.this.dismiss();
			}
		});
		super.setContentView(content);
	}

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		if (TDevice.isTablet(BaseApplication.getIntstance())) {
			int maxWidth = (int) TDevice.dpToPixel(360f);
			if (maxWidth < TDevice.getScreenWidth()) {
				WindowManager.LayoutParams params = getWindow().getAttributes();
				params.width = maxWidth;
				getWindow().setAttributes(params);
			}
		}
	}
	
	

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		this.dismiss();
	}

	public void setContent(View view) {
		setContent(view, contentPadding);
	}

	public void setContent(View view, int padding) {
		FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.MATCH_PARENT,
				FrameLayout.LayoutParams.MATCH_PARENT);
	}

	@Override
	public void setContentView(int i) {
		setContent(null);
	}

	@Override
	public void setContentView(View view) {
		setContentView(null, null);
	}

	@Override
	public void setContentView(View view,
			LayoutParams layoutparams) {
		throw new Error("Dialog: User setContent (View view) instead!");
	}


}
